<?php
    m::c('form');

    $setting = get_option('setting');    
    unset($setting[0]);     

    if ( isset($_POST['name']) and isset($_POST['value']))
    {
        if ( isset( $setting [ $_POST['name'] ]) )
        {
            $setting [ $_POST['name'] ]['value'] = trim($_POST['value']); 
            update_option('setting', $setting) ;  
            echo 'ok';   
        }

        return ;
    }

    if ( isset($_GET['action']) and $_GET['action'] == 'default')
    {
        //группа логирования
        $st =    array('check_log'=> array(
        'title' => 'Общее логирование', 
        'value' => 'true', 
        'type' => 'select',
        'option' => array('true', 'false')),

        'check_task_log'=> array(
        'title' => 'Логирование во время выполнения задач', 
        'value' => 'true', 
        'type' => 'select',   
        'option' => array('true', 'false')
        ) 

        ) ;

        $setting  =  $st;
        update_option('setting', $setting) ;  
    }

?>
<script type="text/javascript">
    $(document).ready(function(){

        $('.setting_class').change( 
        function ()
        { 
            $.ajax({
                type: "POST", url: "admin.php?page=setting&type=ajax", 
                data: "name="+this.name+"&value="+this.value,

                complete: function(data)
                {
                    result = data.responseText;
                }
            });
        } 
        );          

        $('#def').click( 
        function ()
        { 
            var main = $("#main");
            var load = $("#load");


            if ( confirm('Установить настройки по умолчанию?') )
                {
                load.fadeIn('slow'); 
                $.ajax({
                    type: "POST", url: "admin.php?page=setting&type=ajax&action=default", 
                    data: "",

                    complete: function(data)
                    {
                        result = data.responseText;
                        main.fadeOut('slow');   
                        main.html( result );
                        main.fadeIn('slow');

                        load.fadeOut('slow');
                    }
                });
            }
        } 
        );  
    });
</script>
<h2>Настройки</h2> 
<?php


    //add_option('setting', $st);



    if ( is_array($setting) and count($setting) > 0)
    {
        $f = new form(); 
        $f->class='setting_class';
        $color = '';
        foreach ($setting as $name => $value)
        {
            if ($value['type'] == 'select')
            {
                $opt = array();

                foreach ($value['option'] as $va)
                {
                    $sel = '';
                    if ($va == $value['value'] ) $sel = 'selected' ;
                    $opt [] = array(
                    0=> $va,
                    1=> $va,
                    2=> $sel
                    );  
                }

                $color = $color == '#eee' ? '#fff':'#eee';   
                $f->clform[] = '<tr bgcolor="'.$color.'"><td width="100px" align="center">';
                $f->add($value['type'], '', $name,  $value['value'], '</td><td align="left">'.$value['title'].'</td></tr>', $opt);
            }
            // echo $value['value'].' '.$value['title'].'<br>';
        }

        echo '<table class="table_setting" width="100%" border="0" cellpadding="3">';     
        echo $f->get_form();  
        echo '</table>';     
    }
    else
    {
        echo '<br><br><a href="#" id="def">По умолчанию</a>';



    }
?>
